﻿using BaseDll;
using CommonTools;

using HY_UPHMgr;
using ModuleUnit;
using MotionIoLib;
using OtherDevice;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using UserData;
using static UserData.Control_ScrewData;
using TestResult = UserData.TestResult;

namespace StationDemo
{
  
    public class Param_Load
    {

        public ParamSet 键盘模式 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "键盘模式",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 扫码超时时间 = new ParamSet()
        {
            _enuValType = ParamSetUnit.intUnit,
            _strParamUnit = "扫码超时时间",
            _strParamValMax = 600000,
            _strParamValMin = 0,
            _strParamVal = 60000,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 轴到位后延时 = new ParamSet()
        {
            _enuValType = ParamSetUnit.intUnit,
            _strParamUnit = "轴到位后延时",
            _strParamValMax = 600000,
            _strParamValMin = 0,
            _strParamVal = 500,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 读取标定数据后_是否重新点亮 = new ParamSet()
        {
            _enuValType = ParamSetUnit.boolUnit,
            _strParamUnit = "读取标定数据后_是否重新点亮",
            _strParamValMax = true,
            _strParamValMin = false,
            _strParamVal = false,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位1最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位1最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位1最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位1最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位2最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位2最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位2最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位2最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 三角测试点位3最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位3最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位3最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位3最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位4最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位4最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位4最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位4最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 三角测试点位5最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位5最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位5最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位5最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位6最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位6最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位6最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位6最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 三角测试点位7最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位7最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位7最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位7最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
        public ParamSet 三角测试点位8最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位8最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位8最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位8最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位9最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位9最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位9最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位9最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };


        public ParamSet 三角测试点位10最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位10最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位10最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位10最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位11最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位11最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位11最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位11最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位12最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位12最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位12最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位12最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };


        public ParamSet 三角测试点位13最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位13最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位13最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位13最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };


        public ParamSet 三角测试点位14最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位14最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位14最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位14最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位15最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位15最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位15最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位15最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位16最小值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位16最小值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };

        public ParamSet 三角测试点位16最大值 = new ParamSet()
        {
            _enuValType = ParamSetUnit.doubleUnit,
            _strParamUnit = "三角测试点位16最大值",
            _strParamValMax = 1,
            _strParamValMin = -1,
            _strParamVal = -0.3,
            _ParamRight = UserRight.Operator,
            _ParamClass = "综合",
            b_IsStartCheck = true,
        };
     

    }
    public class Point_Load
    {
        public PointInfo 安全位置 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 上下料位置 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");

     
        public PointInfo 测试位置1 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置2 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置3 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置4 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置5 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置6 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置7 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置8 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置9 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置10 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置11 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置12 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置13 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置14 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置15 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");
        public PointInfo 测试位置16 = new PointInfo(0, 0, 0, 0, 0, 0, UserRight.Operator, "综合");


    }
    public class Config_Load
    {


        [Category("工站参数"), DisplayName("1.扫码枪"), Description("扫码枪")]
        public string str_扫码枪 { set; get; } = "";
        [Category("工站参数"), DisplayName("1.NG盒"), Description("NG盒")]
        public string str_NG盒 { set; get; } = "";

    }

  



    public class Station_三角测量 : CommonTools.StationbaseEx<Param_Load, Point_Load, Config_Load>
    {

        public Station_三角测量(CommonTools.Stationbase station, Param_Load param, Point_Load point, Config_Load config) : base(station, param, point, config)
        {

        }
        #region 参数定义
        TestResult testResult = new TestResult();
        int nUPHIndex = 0;
        string strError = "";
        DateTime date_BeginTime = DateTime.Now;
        DateTime uph_BeginTime = DateTime.Now;

        SNScan_Base 扫码枪
        {
            get 
            {
                return DeviceMgr.GetInstance().Function_获取串口(station_Config.str_扫码枪) as Scan_NLSFM430;
            }
        
        }

        ClylinerBase ClylinerBase_NG盒
        {
            get
            {
                return DeviceMgr.GetInstance().Function_获取气缸(station_Config.str_NG盒) as ClylinerBase;
            }

        }

        public enum StationStep
        {
            [Description("0.初始化步骤")]
            Step_Init = 100,
            [Description("1.扫码")]
            Step_扫码,
            [Description("2.启动")]
            Step_启动,
            [Description("3.点亮")]
            Step_点亮,
            [Description("4.读取标定数据")]
            Step_读取标定数据,
            [Description("5.测试")]
            Step_Test,
            [Description("6.下料")]
            Step_下料,
            [Description("14.结束流程")]
            Step_End,
            [Description("15.NG停止流程")]
            Step_NG,
        }
        #endregion

        #region 主流程
        protected override void StationWork(int step)
        {
            try
            {
                
                switch (step)
                {
                    case (int)StationStep.Step_Init:
                        DelCurrentStep();
                        PushMultStep((int)StepInitRun());
                        break;
                    case (int)StationStep.Step_扫码:
                        DelCurrentStep();
                        PushMultStep((int)Step_扫码());
                        break;
                    case (int)StationStep.Step_启动:
                        DelCurrentStep();
                        PushMultStep((int)Step_启动());
                        break;
                    case (int)StationStep.Step_点亮:
                        DelCurrentStep();
                        PushMultStep((int)Step_点亮());
                        break;
                    case (int)StationStep.Step_读取标定数据:
                        DelCurrentStep();
                        PushMultStep((int)Step_读取标定数据());
                        break;
                    case (int)StationStep.Step_Test:
                        DelCurrentStep();
                        PushMultStep((int)Step_Test());
                        break;
                    case (int)StationStep.Step_下料:
                        DelCurrentStep();
                        PushMultStep((int)Step_下料());
                        break;
                    case (int)StationStep.Step_End:
                        DelCurrentStep();
                        PushMultStep((int)StepEnd());
                        Thread.Sleep(10);
                        break;
                    case (int)StationStep.Step_NG:
                        DelCurrentStep();
                        PushMultStep((int)StepNG());
                        Thread.Sleep(10);
                        break;
                }
            }
            catch (Exception ex)
            {
                LogHelper.OnTest_ShowLog(Name, $"{Name} 发生错误:{ex.StackTrace},{ex.Message}", LogType.Err);
                StationMgr.GetInstance().Stop();
            }
        }
        protected override bool InitStation()
        {
          //  ProductDataShow.OnTest_ShowProductData(testResult);

           // LoadSeverMgr.Instance.Start();

            ClearAllStep();
            PushMultStep((int)StationStep.Step_Init);
            return true;
        }
        public override void PauseDeal()
        {

        }
        public override void ResumeDeal()
        {

        }
        public override void StopDeal()
        {

        }

        #endregion


        #region  方法

        public StationStep StepInitRun(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "已启动测试", LogType.Info);
            Function_运动到点位(station_Point.上下料位置, bmanual);
            return StationStep.Step_扫码;
        }

        public StationStep Step_扫码(bool bmanual = false)
        {
            UPHTimeMgr.Instance.Star(nUPHIndex, MergeModel.StandAlone);
            GlobalVariable.启用安全光栅 = false;
            StationStateHelper.OnTest_ShowStep(Name, "请扫码", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"请扫码", LogType.Info);
            string SN = "";
            if (!Function_扫码(ref SN))
            {
                return StationStep.Step_扫码;
            }
            Form_Auto.ShowSN("");
            LogHelper.OnTest_ShowLog(Name, $"SN为:{SN}", LogType.Info);

            UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"扫码完成");

            testResult = new TestResult();
            Function_SetLimit();
            testResult.str_机型.Value = Param_System.Instance.机型._strParamVal;
            testResult.str_批次.Value = Param_System.Instance.批次._strParamVal;
            testResult.str_操作者.Value = Param_System.Instance.操作者._strParamVal;
            testResult.str_设备编号.Value = Param_System.Instance.设备编号._strParamVal;
            testResult.str_工站编号.Value = Param_System.Instance.工站编号._strParamVal;
            testResult.time_当前时间.Value = DateTime.Now;
            testResult.最终结果.Value = "Pass";
            testResult.测试结果.Value = true;
            testResult.str_外壳SN.Value = SN;
            return StationStep.Step_启动;
        }
        public StationStep Step_启动(bool bmanual = false)
        {
            Retry:
            StationStateHelper.OnTest_ShowStep(Name, "请上料，并按双启动按钮", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"请上料，并按双启动按钮", LogType.Info);
          
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            while (true && sys.g_AppMode != AppMode.AirRun)
            {
                if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.双启动信号.ToString(), true, 100))
                {
                    IO_OUT.双启动指示灯.Set(false);
                    break;
                }
                Thread.Sleep(1);
                if (stopwatch.ElapsedMilliseconds > 200)
                {
                    IO_OUT.双启动指示灯.Set(!IO_OUT.双启动指示灯.Get());
                }
            }
            if (sys.g_AppMode == AppMode.AirRun)
            {
                for (int i = 0; i < 3; i++)
                {
                    IO_OUT.双启动指示灯.Set(true);
                    Thread.Sleep(100);
                    IO_OUT.双启动指示灯.Set(false);
                    Thread.Sleep(100);
                }
            }
            else
            {
               //if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.治具开合感应.ToString(), false, 100))
               //{
               //    AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.治具开合感应, new string[] { "重试" }, new string[] { "治具开合感应异常，请检查是否完成合盖操作" });
               //    goto Retry;
               //}
               //if (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.产品有无感应.ToString(), false, 100))
               //{
               //    AlarmMgr.GetIntance().WarnWithDlg((int)AlarmList.产品有无感应, new string[] { "重试" }, new string[] { "产品有无感应异常，请检查是否放料" });
               //    goto Retry;
               //}
            }

            GlobalVariable.启用安全光栅 = true;
            UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"按下双启动");
            return StationStep.Step_点亮;
        }
        public StationStep Step_点亮(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "开始点亮", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"开始点亮", LogType.Info);
            if (sys.g_AppMode != AppMode.AirRun)
            {
                //点亮
                int count = 0;
            re:
                if (!LoadSeverMgr.Instance.Function_点亮(testResult.str_外壳SN.Value))
                {
                    LogHelper.OnTest_ShowLog(Name, $"第{count + 1}次点亮失败", LogType.Info);

                    if (count < 3)
                    {
                        count++;
                        goto re;
                    }
                    testResult.测试结果.Value = false;
                    testResult.FailStep.Value = "点亮";
                    LogHelper.OnTest_ShowLog(Name, $"点亮失败", LogType.Err);
                    StationStateHelper.OnTest_ShowStep(Name, "点亮失败", LogType.Err);
                    return StationStep.Step_下料;
                }
                LogHelper.OnTest_ShowLog(Name, $"点亮成功", LogType.Info);
                UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"点亮成功");
            }
            else
            {
                StationStateHelper.OnTest_ShowStep(Name, "空跑，已屏蔽点亮", LogType.Info);
                LogHelper.OnTest_ShowLog(Name, $"空跑，已屏蔽点亮", LogType.Info);
            }
            return StationStep.Step_读取标定数据;
        }


        public StationStep Step_读取标定数据(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "开始获取标定数据", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"开始获取标定数据", LogType.Info);

            if (sys.g_AppMode != AppMode.AirRun)
            {
                if (!LoadSeverMgr.Instance.Function_读取标定数据())
                {

                    LogHelper.OnTest_ShowLog(Name, $"Function_读取标定数据失败", LogType.Err);
                    StationStateHelper.OnTest_ShowStep(Name, "Function_读取标定数据失败", LogType.Err);
                    testResult.测试结果.Value = false;
                    testResult.FailStep.Value = "读取标定数据";
                    return StationStep.Step_下料;
                }
                else                
                {
                    StationStateHelper.OnTest_ShowStep(Name, "开始获取标定数据完成", LogType.Info);
                    LogHelper.OnTest_ShowLog(Name, $"开始获取标定数据完成", LogType.Info);
                }

                if (station_Param.读取标定数据后_是否重新点亮._strParamVal)
                {
                    StationStateHelper.OnTest_ShowStep(Name, "重新点亮", LogType.Info);
                    LogHelper.OnTest_ShowLog(Name, $"重新点亮", LogType.Info);
                    LoadSeverMgr.Instance.Functio_停止取图();
                    Thread.Sleep(500);
                    //点亮
                    int count = 0;
                re:
                    if (!LoadSeverMgr.Instance.Function_点亮(testResult.str_外壳SN.Value))
                    {
                        LogHelper.OnTest_ShowLog(Name, $"第{count + 1}次点亮失败", LogType.Info);

                        if (count < 3)
                        {
                            count++;
                            goto re;
                        }
                        testResult.测试结果.Value = false;
                        testResult.FailStep.Value = "点亮";
                        LogHelper.OnTest_ShowLog(Name, $"点亮失败", LogType.Err);
                        StationStateHelper.OnTest_ShowStep(Name, "点亮失败", LogType.Err);
                        return StationStep.Step_下料;
                    }
                    LogHelper.OnTest_ShowLog(Name, $"重新点亮成功", LogType.Info);
                    UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"重新点亮成功");
                }
            }
            else
            {
                StationStateHelper.OnTest_ShowStep(Name, "空跑，已屏蔽获取标定数据", LogType.Info);
                LogHelper.OnTest_ShowLog(Name, $"空跑，已屏蔽获取标定数据", LogType.Info);
            }
            return StationStep.Step_Test;
        }

        public StationStep Step_Test(bool bmanual = false)
        {
  
            StationStateHelper.OnTest_ShowStep(Name, "开始测试", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"开始测试", LogType.Info);
            double[] data = new double[16];
            PointInfo point = station_Point.测试位置1;
            LogHelper.OnTest_ShowLog(Name, $"测试位置：测试位置1 U轴目标位置：{point.pointU},Tx轴目标位置：{point.pointTx}", LogType.Err);
            Function_运动到点位(point, bmanual);
            Thread.Sleep(station_Param.轴到位后延时._strParamVal);
            if (sys.g_AppMode != AppMode.AirRun)
                LoadSeverMgr.Instance.Function_三角取图(0);
            FieldInfo[] fields = station_Point.GetType().GetFields();
            for (int i = 1; i < 16; i++)
            {
                foreach (FieldInfo temp in fields)
                {
                    if (temp.Name == $"测试位置{i + 1}")
                    {
                        PointInfo pointInfo = (PointInfo)temp.GetValue(station_Point);
                        LogHelper.OnTest_ShowLog(Name, $"测试位置：{temp.Name} U轴目标位置：{pointInfo.pointU},Tx轴目标位置：{pointInfo.pointTx}", LogType.Err);
                        Function_运动到点位(pointInfo, bmanual);
                        data[i - 1] = i - 1;
                        if (sys.g_AppMode != AppMode.AirRun)
                            LoadSeverMgr.Instance.Function_三角标定(ref data[i - 1]);
                        Thread.Sleep(station_Param.轴到位后延时._strParamVal);
                        if (sys.g_AppMode != AppMode.AirRun)
                            LoadSeverMgr.Instance.Function_三角取图(i);
                    }
                }
            }
            data[15] = 15;
            if (sys.g_AppMode != AppMode.AirRun)
                LoadSeverMgr.Instance.Function_三角标定(ref data[15]);
           
            testResult.ErrValue1.Value = data[0];
            testResult.ErrValue2.Value = data[1];
            testResult.ErrValue3.Value = data[2];
            testResult.ErrValue4.Value = data[3];
            testResult.ErrValue5.Value = data[4];
            testResult.ErrValue6.Value = data[5];
            testResult.ErrValue7.Value = data[6];
            testResult.ErrValue8.Value = data[7];
            testResult.ErrValue9.Value = data[8];
            testResult.ErrValue10.Value = data[9];
            testResult.ErrValue11.Value = data[10];
            testResult.ErrValue12.Value = data[11];
            testResult.ErrValue13.Value = data[12];
            testResult.ErrValue14.Value = data[13];
            testResult.ErrValue15.Value = data[14];
            testResult.ErrValue16.Value = data[15];

            if (!Func_数据对比(testResult,ref strError))
            {
                testResult.测试结果.Value = false;
                testResult.FailStep.Value = strError;
                LogHelper.OnTest_ShowLog(Name, $"三角标定测试失败", LogType.Err);
                UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"测试完成");
            }

            LogHelper.OnTest_ShowLog(Name, $"测试完成", LogType.Info);
            UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"测试完成");


            StationStateHelper.OnTest_ShowStep(Name, "预备到下料位", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"预备到下料位", LogType.Info);
            return StationStep.Step_下料;
        }
        public StationStep Step_下料(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "开始下料", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"开始下料", LogType.Info);


            Function_运动到点位(station_Point.上下料位置);
            if (sys.g_AppMode != AppMode.AirRun)
            {
                GlobalVariable.启用安全光栅 = false;

                LoadSeverMgr.Instance.Functio_停止取图();

                double d_CTTime = (DateTime.Now - date_BeginTime).TotalSeconds;
                double d_uphTime = (DateTime.Now - uph_BeginTime).TotalSeconds;
                date_BeginTime = DateTime.Now;
                uph_BeginTime = DateTime.Now;
                testResult.time_结束时间.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ProductDataShow.OnTest_ShowProductData(testResult);
                if (!testResult.测试结果.Value)
                {
                    IO_OUT.双色灯绿.Set(false);
                    IO_OUT.双色灯红.Set(true);
                    Form_Auto.FlashResult(strError);
                    ClylinerBase_NG盒?.Function_后退();
                    testResult.最终结果.Value = "Fail";
                    StationStateHelper.OnTest_ShowStep(Name, "请下NG料", LogType.Warn);
                   // PathHelper.CopyDir(PathHelper.ImageOKPath, PathHelper.ImageNGPath);
                    while (IOMgr.GetInstace().Function_ReCheckIO(IO_IN.NG光栅1.ToString(),false,200) || IOMgr.GetInstace().Function_ReCheckIO(IO_IN.NG光栅2.ToString(), false, 200))
                    {
                        if (Param_System.Instance.是否屏蔽NG盒._strParamVal)
                        {
                            break;
                        }
                        Thread.Sleep(100);
                    }

                    StationStateHelper.OnTest_ShowStep(Name, $"三角标定完成OK,CT:{d_CTTime.ToString("0.00")}", LogType.Info);
                    LogHelper.OnTest_ShowLog(Name, $"三角标定完成OK,CT:{d_CTTime.ToString("0.00")}", LogType.Info);
                }
                else
                {
                    IO_OUT.双色灯绿.Set(true);
                    IO_OUT.双色灯红.Set(false);
                    Form_Auto.FlashResult("Pass");
                    StationStateHelper.OnTest_ShowStep(Name, $"三角标定完成NG,CT:{d_CTTime.ToString("0.00")}", LogType.Err);
                    LogHelper.OnTest_ShowLog(Name, $"三角标定完成NG，SN码-{testResult.str_外壳SN.Value},CT:{d_CTTime.ToString("0.00")}", LogType.Err);
                }
                string path = $"D:\\HY System\\结果\\{Param_System.Instance.机型._strParamVal}\\{Param_System.Instance.批次._strParamVal}\\" +
            $"\\{DateTime.Now.ToString("yyyy-MM-dd")}\\三角测量\\测试数据.csv";
                if (!Directory.Exists(Directory.GetParent(path).FullName))
                {
                    Directory.CreateDirectory(Directory.GetParent(path).FullName);
                }
                CSVHelper.Instance.SaveToCSVPath(path, testResult, true);

                if (d_uphTime > Param_System.Instance.正常生产时长限制._strParamVal)
                {
                    TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", 0, testResult.测试结果.Value);
                }
                else
                {
                    TestTimeMgr.GetInstance().Function_AddTestTime("数据统计", d_uphTime, testResult.测试结果.Value);
                }
            }
            else
            {
                LogHelper.OnTest_ShowLog(Name, $"空跑，已屏蔽测试", LogType.Info);
            }


            LogHelper.OnTest_ShowLog(Name, $"测试完成", LogType.Info);
            UPHTimeMgr.Instance.AddData(nUPHIndex, Name, $"测试完成");


           
            return StationStep.Step_End;
        }

        public StationStep StepEnd(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "流程结束", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"流程结束", LogType.Info);
           

            return StationStep.Step_扫码;
        }
        public StationStep StepNG(bool bmanual = false)
        {
            StationStateHelper.OnTest_ShowStep(Name, "流程NG", LogType.Info);
            LogHelper.OnTest_ShowLog(Name, $"流程NG", LogType.Info);

            return StationStep.Step_End;
        }
        public bool Function_工站回零()
        {
            StationStateHelper.OnTest_ShowStep(Name, $"正在回零", LogType.Info);
            bool bResult = true;

            Task task_Load_AxisTx = Task.Run(() =>
            {
                try
                {
                    UserDefineFunction.Axis_回零(AxisTx, "Tx轴", this, true);
                }
                catch (Exception)
                {
                    bResult = false;
                }
            });

            Task task_Load_AxisU = Task.Run(() =>
            {
                try
                {
                    UserDefineFunction.Axis_回零(AxisU, "旋转平台", this, true);
                }
                catch (Exception e)
                {
                    bResult = false;
                }
            });

            Task task_Load_AxisTy = Task.Run(() =>
            {
                try
                {
                    UserDefineFunction.Axis_回零(AxisTy, "微动平台", this, true);
                }
                catch (Exception e)
                {
                    bResult = false;
                }
            });


            Task.WaitAll(task_Load_AxisTx, task_Load_AxisU, task_Load_AxisTy);





            StationStateHelper.OnTest_ShowStep(Name, $"回零完成", LogType.Info);
            return bResult;
        }
        public bool Function_气缸复位(bool bmanual = false)
        {

            return true;
        }
        public bool Function_气缸回安全位(bool bmanual = false)
        {

            return true;
        }
        public void Function_运动到点位(PointInfo point, bool bmanual = false)
        {

            MoveMulitAxisPosWaitInpos(new int[] { AxisU, AxisTx, AxisTy }, new double[] { point.pointU, point.pointTx, point.pointTy }, new double[] { (double)SpeedType.High, (double)SpeedType.High, (double)SpeedType.High }, 0.5, bmanual, this);

        }



        public bool Function_扫码(ref string strSN)
        {
            if (sys.g_AppMode == AppMode.AirRun)
            {
                strSN = "空跑";
                return true;
            }
            if (Param_System.Instance.是否屏蔽扫码._strParamVal)
            {
                strSN = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
                return true;
            }
            if (station_Param.键盘模式._strParamVal)
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                while (true)
                {
                    if (GlobalVariable.g_StationState == StationState.StationStateEmg)
                        return false;
                     if (UserConfig.b_isScan)
                    {
                        strSN = UserConfig.str_SN;
                        if (strSN == string.Empty || strSN.Length != Param_System.Instance.SN长度._strParamVal || !strSN.Contains(Param_System.Instance.SN固定字符._strParamVal))
                        {
                            AlarmMgr.GetIntance().WarnWithDlg(AlarmList.产品SN校验错误.GetHashCode(), new string[] { "重试" }, new string[] { $"产品SN校验错误，读取内容=[{strSN}]" });
                           // SNHelper.OnTest_ReflushSN("SN");

                            return false;
                        }
                        else
                        {

                            LogHelper.OnTest_ShowLog(Name, $"读取产品SN=[{strSN}]", LogType.Info);
                            LogHelper.OnTest_ShowLog(Name, $"等待双按钮启动", LogType.Warn);
                            StationStateHelper.OnTest_ShowStep(Name, $"{Name} 等待双按钮启动", LogType.Warn);
                            break;

                        }
                    }
                    Thread.Sleep(10);
                    if (stopwatch.ElapsedMilliseconds > station_Param.扫码超时时间._strParamVal)
                    {
                        AlarmMgr.GetIntance().WarnWithDlg(AlarmList.等到扫码超时.GetHashCode(), new string[] { "重试" }, new string[] { $"SN读取超时，读取内容=[{strSN}]，请重试" });
                        stopwatch.Restart();
                    }
                }

            }
            else
            {
                if (扫码枪?.Fucntion_获取SN码(Param_System.Instance.SN长度._strParamVal, ref strSN, station_Param.扫码超时时间._strParamVal) == 0)
                {
                    strSN = strSN.Replace("\r","");
                    if (strSN.Contains(Param_System.Instance.SN固定字符._strParamVal))
                    {
                        
                        SNHelper.OnTest_ShowSN("SN", strSN);
                        LogHelper.OnTest_ShowLog(Name, $"读取产品SN=[{strSN}]", LogType.Info);

                        StationStateHelper.OnTest_ShowStep(Name, $"{Name} 等待双按钮启动", LogType.Warn);
                        return true;
                    }
                    else
                    {
                        AlarmMgr.GetIntance().WarnWithDlg(AlarmList.产品SN校验错误.GetHashCode(), new string[] { "重试" }, new string[] { $"产品SN校验错误，读取内容=[{strSN}]" });
                        return false;
                    }

                }
                else
                {
                    AlarmMgr.GetIntance().WarnWithDlg(AlarmList.等到扫码超时.GetHashCode(), new string[] { "重试" }, new string[] { $"SN读取超时，读取内容=[{strSN}]，请重试" });
                    return false;
                }

            }
            return true;

        }

     




        public void Function_SetLimit()
        {
            testResult.ErrValue1.Max = $"{station_Param.三角测试点位1最大值._strParamVal}";
            testResult.ErrValue1.Min = $"{station_Param.三角测试点位1最小值._strParamVal}";
            testResult.ErrValue2.Max = $"{station_Param.三角测试点位2最大值._strParamVal}";
            testResult.ErrValue2.Min = $"{station_Param.三角测试点位2最小值._strParamVal}";
            testResult.ErrValue3.Max = $"{station_Param.三角测试点位3最大值._strParamVal}";
            testResult.ErrValue3.Min = $"{station_Param.三角测试点位3最小值._strParamVal}";
            testResult.ErrValue4.Max = $"{station_Param.三角测试点位4最大值._strParamVal}";
            testResult.ErrValue4.Min = $"{station_Param.三角测试点位4最小值._strParamVal}";

            testResult.ErrValue5.Max = $"{station_Param.三角测试点位5最大值._strParamVal}";
            testResult.ErrValue5.Min = $"{station_Param.三角测试点位5最小值._strParamVal}";
            testResult.ErrValue6.Max = $"{station_Param.三角测试点位6最大值._strParamVal}";
            testResult.ErrValue6.Min = $"{station_Param.三角测试点位6最小值._strParamVal}";
            testResult.ErrValue7.Max = $"{station_Param.三角测试点位7最大值._strParamVal}";
            testResult.ErrValue7.Min = $"{station_Param.三角测试点位7最小值._strParamVal}";
            testResult.ErrValue8.Max = $"{station_Param.三角测试点位8最大值._strParamVal}";
            testResult.ErrValue8.Min = $"{station_Param.三角测试点位8最小值._strParamVal}";


            testResult.ErrValue9.Max = $"{station_Param.三角测试点位9最大值._strParamVal}";
            testResult.ErrValue9.Min = $"{station_Param.三角测试点位9最小值._strParamVal}";
            testResult.ErrValue10.Max = $"{station_Param.三角测试点位10最大值._strParamVal}";
            testResult.ErrValue10.Min = $"{station_Param.三角测试点位10最小值._strParamVal}";
            testResult.ErrValue11.Max = $"{station_Param.三角测试点位11最大值._strParamVal}";
            testResult.ErrValue11.Min = $"{station_Param.三角测试点位11最小值._strParamVal}";
            testResult.ErrValue12.Max = $"{station_Param.三角测试点位12最大值._strParamVal}";
            testResult.ErrValue12.Min = $"{station_Param.三角测试点位12最小值._strParamVal}";


            testResult.ErrValue13.Max = $"{station_Param.三角测试点位13最大值._strParamVal}";
            testResult.ErrValue13.Min = $"{station_Param.三角测试点位13最小值._strParamVal}";
            testResult.ErrValue14.Max = $"{station_Param.三角测试点位14最大值._strParamVal}";
            testResult.ErrValue14.Min = $"{station_Param.三角测试点位14最小值._strParamVal}";
            testResult.ErrValue15.Max = $"{station_Param.三角测试点位15最大值._strParamVal}";
            testResult.ErrValue15.Min = $"{station_Param.三角测试点位15最小值._strParamVal}";
            testResult.ErrValue16.Max = $"{station_Param.三角测试点位16最大值._strParamVal}";
            testResult.ErrValue16.Min = $"{station_Param.三角测试点位16最小值._strParamVal}";

        }


        public bool Func_数据对比<T>(T testData, ref string strError)
        {

            {
                //1.获取数据值的类型
                //2.看数据能否转为double或int型，能转就比较，不能上下限就/
                //3.再看上下限能否转为double或int型,能转就比较
                bool result = true;


                PropertyInfo[] spData = testData.GetType().GetProperties();
                Type type = testData.GetType();
                int row = -1;
                foreach (var sp in spData)
                {
                  
                    //获取点检数据
                    object obj = type.GetProperty(sp.Name).GetValue(testData, null);
                    TestInfo tempSp = obj as TestInfo;
                    double d_UpLimit = 0;
                    double d_DownLimit = 0;
                
                    //判断值类型及比较结果
                    bool re = true;
                    //测试内容

                    if (tempSp.Value.GetType() == typeof(double))
                    {
                        //上限比较
                        if (double.TryParse(tempSp.Max, out d_UpLimit))
                        {
                            re &= tempSp.Value <= d_UpLimit;
                        }
                        //下限比较
                        if (double.TryParse(tempSp.Min, out d_DownLimit))
                        {
                            re &= tempSp.Value >= d_DownLimit;
                        }
 
                    }               
                    if (!re)
                    {
                        result = false;
                        strError = $"{tempSp.Name} 失败";
                        break;
                    }


                }

                return result;
            }

        }
        #endregion



    }
}
